package com.example.note.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.note.R;
import com.example.note.adapter.ReminderAdapter;
import com.example.note.database.ReminderEntity;
import com.example.note.viewmodel.ReminderViewModel;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;

import java.util.List;

public class ReminderActivity extends AppCompatActivity {
    
    private MaterialToolbar toolbar;
    private TabLayout tabLayout;
    private RecyclerView recyclerView;
    private TextView emptyStateText;
    private FloatingActionButton fabAdd;
    
    private ReminderViewModel reminderViewModel;
    private ReminderAdapter reminderAdapter;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reminder);
        
        initViews();
        setupToolbar();
        setupRecyclerView();
        setupViewModel();
        setupListeners();
        observeData();
    }
    
    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        tabLayout = findViewById(R.id.tabLayout);
        recyclerView = findViewById(R.id.recyclerView);
        emptyStateText = findViewById(R.id.emptyStateText);
        fabAdd = findViewById(R.id.fabAdd);
    }
    
    private void setupToolbar() {
        setSupportActionBar(toolbar);
        toolbar.setTitle("备忘录");
        toolbar.setNavigationOnClickListener(v -> finish());
    }
    
    private void setupRecyclerView() {
        reminderAdapter = new ReminderAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(reminderAdapter);
        
        reminderAdapter.setOnReminderClickListener(reminder -> {
            Intent intent = new Intent(this, ReminderEditActivity.class);
            intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, reminder.getId());
            startActivity(intent);
        });
        
        reminderAdapter.setOnReminderCompleteListener(reminder -> {
            reminderViewModel.markAsCompleted(reminder.getId());
        });
    }
    
    private void setupViewModel() {
        reminderViewModel = new ViewModelProvider(this).get(ReminderViewModel.class);
    }
    
    private void setupListeners() {
        fabAdd.setOnClickListener(v -> {
            Intent intent = new Intent(this, ReminderEditActivity.class);
            startActivity(intent);
        });
        
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0: // 活跃的备忘录
                        observeActiveReminders();
                        break;
                    case 1: // 已完成的备忘录
                        observeCompletedReminders();
                        break;
                }
            }
            
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {}
            
            @Override
            public void onTabReselected(TabLayout.Tab tab) {}
        });
    }
    
    private void observeData() {
        // 默认显示活跃的备忘录
        observeActiveReminders();
    }
    
    private void observeActiveReminders() {
        reminderViewModel.getActiveReminders().observe(this, this::updateUI);
    }
    
    private void observeCompletedReminders() {
        reminderViewModel.getCompletedReminders().observe(this, this::updateUI);
    }
    
    private void updateUI(List<ReminderEntity> reminders) {
        if (reminders == null || reminders.isEmpty()) {
            recyclerView.setVisibility(View.GONE);
            emptyStateText.setVisibility(View.VISIBLE);
            emptyStateText.setText("暂无备忘录");
        } else {
            recyclerView.setVisibility(View.VISIBLE);
            emptyStateText.setVisibility(View.GONE);
            reminderAdapter.setReminders(reminders);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // 刷新数据
        if (tabLayout.getSelectedTabPosition() == 0) {
            observeActiveReminders();
        } else {
            observeCompletedReminders();
        }
    }
}